# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# 飞猪旅行门票
# https://btrip.alitrip.com/piao/
# scrapy crawl ali_trip
class AliTripSpider(scrapy.Spider):
    name = 'ali_trip'

    allowed_domains = ['btrip.alitrip.com']

    start_urls = ['http://btrip.alitrip.com/']

    custom_settings = {
        'ITEM_PIPELINES': {
            'messAround.pipeline.ali.AliTripPipeline': 300
        }
    }

    def start_requests(self):
        num = 100
        # 排行
        urls = [
            {
                "name": "国内门票",
                "category": "china",
                "category_id": 1,
                "api": f'https://s.alitrip.com/scenic/ajax/hotScenic.htm?num={num}&format=json&type=china'
            },
            {
                "name": "国外门票",
                "category": "abroad",
                "category_id": 2,
                'api': f'https://s.alitrip.com/scenic/ajax/hotScenic.htm?num={num}&format=json&type=abroad'
            },
        ]

        for url in urls:
            yield scrapy.Request(url=url['api'],
                                 meta={"name": url['name'], "category": url['category'],
                                       "category_id": url['category_id']},
                                 callback=self.parse,
                                 headers=util.default_headers)

    def parse(self, response):
        cover_size = '250x250'
        result = util.make_json(response.text)
        if result['code'] == 200:
            for key, val in enumerate(result['data']):
                yield {
                    'no': key + 1,
                    'category': response.meta['category'],
                    'category_id': response.meta['category_id'],
                    'category_name': response.meta['name'],
                    'name': val['name'],
                    'link': f'http://s.fliggy.com/scenic/detail.htm?tbpm=3&sid={val["scenicId"]}',
                    'cover': f'http://gtd.alicdn.com/bao/uploaded/{val["pic_url"]}_{cover_size}',
                    'price': val['price'],
                    'sell_count': int(val['sell_count'])
                }
            pass
